<?php
class M_qin_request extends Cemvc_Db_MysqlDb{
  public $PrimaryKey='id';
  /*CEPHP会在要需要时查询主键，但主动设置主键可以使程序执行更快*/

  public function __construct()
  {
    parent::__construct();
  }

  /* return the request detail with the specified id */
  public function get_req_info($id)
  {
    return $this->where(array("id = $id"))->fetchOne();
  }
  public function agree_request($id)
  {
    return $this->where(array("id = $id"))->update(array("state"=>REQUEST_AGREE));
  }
  public function cancel_request($id)
  {
    return $this->where(array("id = $id"))->update(array("state"=>REQUEST_CANCEL));
  }  
  /* get all the request to userid and users created by userid */
  public function get_request($userid)
  {
    $user = new M_qin_users();
    $res = $user->get_users(array("creator = $userid or id = $userid"));
    $users = M_Tools::convert_mysql_results_to_array($res, "id");

    return $this->where(array("request.rel_id in (".implode(', ',array_keys($users)).")", "state = '".REQUEST_INIT."'"))->fetchAll();
  }

  public function insert_request($array)
  {
    return $this->insert(array("user_id"=>$array["user_id"],
			       "user_name"=>$array["user_name"],
			       "rel_id" => $array["rel_id"],
			       "rel_name" => $array["rel_name"],
			       "rel_type"=>$array["rel_type"],
			       "message"=>$array["message"]));  
  }
}